home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / gpplib22.zoo / libsrc / timer.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  3.3 KB  |  130 lines

  1. /* 
  2. Copyright (C) 1990, 1992 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of the GNU C++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17.  
  18. #ifdef __GNUG__
  19. #pragma implementation
  20. #endif
  21. #include <builtin.h>
  22.  
  23. // Timing functions from Doug Schmidt...
  24.  
  25. /* no such thing as "negative time"! */
  26. #define  TIMER_ERROR_VALUE -1.0   
  27.  
  28. // If this does not work on your system, change this to #if 0, and 
  29. // report the problem
  30.  
  31. #if 1
  32.  
  33. #include <g_config.h>
  34. #include <osfcn.h>
  35. #if !_G_HAVE_SYS_RESOURCE || !defined(RUSAGE_SELF)
  36. #define USE_TIMES
  37. #include <sys/param.h>
  38. #include <sys/times.h>
  39. #if !defined (HZ) && defined(CLK_TCK)
  40. #define HZ CLK_TCK
  41. #endif
  42. static struct tms Old_Time;
  43. static struct tms New_Time;
  44. #else
  45. static struct rusage Old_Time;
  46. static struct rusage New_Time;
  47. #endif
  48. static int    Timer_Set = 0;
  49.  
  50. double start_timer()
  51. {
  52.    Timer_Set = 1;
  53. #ifdef USE_TIMES
  54.    times(&Old_Time);
  55.    return((double) Old_Time.tms_utime / HZ);
  56. #else
  57.    getrusage(RUSAGE_SELF,&Old_Time);        /* set starting process time */
  58.    return(Old_Time.ru_utime.tv_sec + (Old_Time.ru_utime.tv_usec / 1000000.0));
  59. #endif
  60. }
  61.  
  62. /* Returns process time since Last_Time.
  63.    If parameter is 0.0, returns time since the Old_Time was set.
  64.    Returns TIMER_ERROR_VALUE if `start_timer' is not called first.  */
  65.  
  66. double return_elapsed_time(double Last_Time)
  67. {
  68.    if (!Timer_Set) {
  69.       return(TIMER_ERROR_VALUE);
  70.    }   
  71.    else {
  72.     /* get process time */
  73. #ifdef USE_TIMES
  74.       times(&New_Time);
  75. #else
  76.       getrusage(RUSAGE_SELF,&New_Time);
  77. #endif
  78.       if (Last_Time == 0.0) {
  79. #ifdef USE_TIMES
  80.      return((double) (New_Time.tms_utime - Old_Time.tms_utime) / HZ);
  81. #else
  82.          return((New_Time.ru_utime.tv_sec - Old_Time.ru_utime.tv_sec) + 
  83.                ((New_Time.ru_utime.tv_usec - Old_Time.ru_utime.tv_usec) 
  84.                 / 1000000.0));
  85. #endif
  86.       }
  87.       else {
  88. #ifdef USE_TIMES
  89.      return((double) New_Time.tms_utime / HZ - Last_Time);
  90. #else
  91.          return((New_Time.ru_utime.tv_sec + 
  92.                 (New_Time.ru_utime.tv_usec / 1000000.0)) - Last_Time);
  93. #endif
  94.       }
  95.    }
  96. }
  97.  
  98. #ifdef VMS
  99. void sys$gettim(unsigned int*) asm("sys$gettim");
  100.  
  101. getrusage(int dummy,struct rusage* time){
  102.     double rtime;
  103.     unsigned int systime[2];
  104.     int i;
  105.     sys$gettim(&systime[0]);
  106.     rtime=systime[1];
  107.     for(i=0;i<4;i++) rtime *= 256;
  108.     rtime+= systime[0];
  109. /* we subtract an offset to make sure that the number fits in a long int*/
  110.     rtime=rtime/1.0e+7-4.144e+9;
  111.     time->ru_utime.tv_sec= rtime;
  112.     rtime=(rtime-time->ru_utime.tv_sec)*1.0e6;    
  113.     time->ru_utime.tv_usec= rtime;
  114. }
  115. #endif
  116. #else /* dummy them out */
  117.  
  118. double start_timer()
  119. {
  120.   return TIMER_ERROR_VALUE;
  121. }
  122.  
  123. double return_elapsed_time(double)
  124. {
  125.   return TIMER_ERROR_VALUE;
  126. }
  127.  
  128. #endif /* timing stuff */
  129.  
  130.